package sluentities;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.taglibs.standard.tag.common.core.ForEachSupport;
import org.bson.types.ObjectId;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import org.mongodb.morphia.query.Query;
import config.MongoDB;

/**
 * Created by chenhong on 2017/2/27.
 */

@Entity(value = "variables", noClassnameStored = true)
public class GrammarVariableEntity {
    @Id
    protected ObjectId id;
    protected String name;
    protected String type;
    protected String scope;
    protected String defaultValue;
    protected String reply;
    protected List<String> defaultFunction=new ArrayList<String>();
    protected String dynamicFunName;//query
    protected List<String> paramlists = new ArrayList<String>();

    public GrammarVariableEntity() {
        super();
    }

    public void setId(ObjectId id) {
        this.id = id;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    public void setLists(List<String> lists) {
        this.paramlists = lists;
    }

    public ObjectId getId() {
        return id;
    }

    public List<String> getLists() {
        return paramlists;
    }

    public String getListsString(){
        String s="";
        for (int i=0;i<paramlists.size();i++) {
            s=s+paramlists.get(i)+",";
        }
        if(paramlists.size()>0)s=s.substring(0,s.length()-1);
        return s;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(String value) { this.defaultValue = value; }

    public String getReply(){return reply;}

    public void setReply(String reply){this.reply=reply;}

    public List<String> getDefaultFunction(){return defaultFunction;}

    public String getDefaultFunctionString(){
        String s="";
        for (String ss:defaultFunction)s=s+ss+",";
        if(defaultFunction.size()>0)s=s.substring(0,s.length()-1);
        return s;
    }

    public void setDefaultFunction(List<String> defaultFunction){this.defaultFunction=defaultFunction;}

    public String getDynamicFunName(){return dynamicFunName;}

    public void setDynamicFunName(String dynamicFunName){this.dynamicFunName=dynamicFunName;}

    public GrammarVariableEntity(String name) {
        super();
        this.name = name;
    }

    public GrammarVariableEntity(ObjectId id) {
        super();
        this.id = id;
    }

    public GrammarVariableEntity(ObjectId id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public GrammarVariableEntity remove(){

        Datastore mongoDatastore = MongoDB.instance().getDatabase();
        Query<GrammarRuleEntity> q = mongoDatastore.createQuery(GrammarRuleEntity.class);
        for (GrammarRuleEntity rule:q){
            boolean del=false;
            for (RuleEntryEntity entry:rule.getLists()){
                if(entry.getEntry().equals(id)){
                    del=true;
                    break;
                }
            }
            if(del)mongoDatastore.delete(rule.remove());
        }
        Query<GrammarVariableEntity> qg=mongoDatastore.createQuery(GrammarVariableEntity.class).field("paramlists").equal(name);
        for(GrammarVariableEntity gve:qg){
            Iterator<String> it = gve.getLists().iterator();
            while (it.hasNext()){
                if(it.next().equals(name)){
                    it.remove();
                }
            }
            mongoDatastore.save(gve);
        }
        Query<NavigationEntity> qn = mongoDatastore.createQuery(NavigationEntity.class).field("params").equal(id);
        for (NavigationEntity n:qn){
            Iterator<GrammarVariableEntity> it = n.getParams().iterator();
            while (it.hasNext()){
                if(it.next().getId().equals(id)){
                    it.remove();
                }
            }
            mongoDatastore.save(n);
        }
        return this;
    }
}
